<template>
  <div>

    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>网站管理</el-breadcrumb-item>
      <el-breadcrumb-item>广告管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">

      <el-dialog title="添加广告" :title="posterForm.id==null?'添加广告':'编辑广告'" :visible.sync="dialogFormVisible" :append-to-body=true>

        <el-form :model="posterForm"  ref="userForm" label-width="80px">

          <el-row :gutter="22">
            <el-col :span="11">
              <el-form-item label="图片">
                <el-upload
                  class="avatar-uploader"
                  action="/api/education-website/eduWebsite/upload"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload">
                  <img v-if="posterForm.picPath!=null" :src="'/api/education-website/eduWebsite/download/'+posterForm.picPath" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>


          <el-row :gutter="22">
            <el-col :span="11">
              <el-form-item label="广告类型" prop="adType">
                <el-select v-model="posterForm.adType" placeholder="请选择活动区域">
                  <el-option label="轮播图" value="0"></el-option>
                  <el-option label="楼层广告" value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="广告名称" prop="adName">
                <el-input v-model="posterForm.adName" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="22">
            <el-col :span="11">
              <el-form-item label="广告标题" prop="adTitle">
                <el-input v-model="posterForm.adTitle" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
              <el-col :span="11">
                <el-form-item label="选择模板">
                  <el-select v-model="posterForm.templateid" placeholder="请选择">
                    <el-option
                      v-for="item in TsList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row :gutter="22">
            <el-col :span="11">
              <el-form-item label="跳转地址" prop="url">
                <el-input v-model="posterForm.url" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="排序" prop="sort">
                <el-input v-model="posterForm.sort" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>




      <el-form :model="poster" class="demo-form-inline" >
        <el-row :gutter="20">
          <el-form-item>
            <el-col :span="4">
              <el-input placeholder="关键字" v-model="poster.name"></el-input>
            </el-col>
            <el-button plain type="info" @click="searchList">搜索</el-button>
            <el-button type="primary" @click="addSave">添加广告</el-button>
          </el-form-item>
        </el-row>
      </el-form>



      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="adName"
          label="名称"
          width="150">
        </el-table-column>
        <el-table-column
          prop="adTitle"
          label="标题"
          width="150">
        </el-table-column>
        <el-table-column
          label="图片"
          width="150">
          <template slot-scope="scope">
            <img  v-if="scope.row.picPath!=null" :src="'/api/education-website/eduWebsite/download/'+scope.row.picPath" width="50px" height="50px">
          </template>
        </el-table-column>
        <el-table-column
          label="类型"
          width="150">
          <template slot-scope="scope">
            {{scope.row.adType|adTypeFit}}
          </template>
        </el-table-column>
        <el-table-column
          prop="url"
          label="跳转地址"
          width="150">
        </el-table-column>
        <el-table-column
          prop="sort"
          label="排序"
          width="150">
        </el-table-column>
        <el-table-column
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="updatePoster(scope.row.id)">修改</el-button>
            <el-button size="small" type="text" @click="deletePoster(scope.row.id)">删除</el-button></template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

    </el-card>


  </div>
</template>

<script>
  export default {
    name: 'Poster',
    data(){
      return{
        tableData:[],
        poster:{},
        posterForm:{picPath:''},
        dialogFormVisible:false,
        currentPage:0,
        pageSize:5,
        total:0,
        TsList:[],
      }
    },
    methods:{
      // image:function(url){
      //   location.href=url;
      // },
      searchList:function () {
        this.$http.post("/education-website/eduWebsite/list/"+this.currentPage+"/"+this.pageSize,this.poster).then((res)=>{
          this.tableData=res.data.result.list;
          this.total=res.data.result.total;
        })
      },
      //添加广告
      addSave:function () {
        this.dialogFormVisible=true;
      },
      //保存广告
      save:function () {
        this.$http.post("/education-website/eduWebsite/addSave",this.posterForm).then((res)=>{
          if(res.data.success){
            this.$message.success(res.data.message);
            this.posterForm={}
            this.searchList();
          }else{
            this.$message.error(res.data.message);
          }
          this.dialogFormVisible=false;
          this.searchList();
        })
      },
      //修改广告
      updatePoster:function (id) {
        this.dialogFormVisible=true;
        this.$http.get("/education-website/eduWebsite/findPoster/"+id).then((res)=>{
          if(res.data.success){
            this.posterForm=res.data.result;
          }else{
            this.$message.error(res.data.message);
          }
          this.searchList();
        })
      },
      //删除广告
      deletePoster:function (id) {
        this.$http.get("/education-website/eduWebsite/deletePoster/"+id).then((res)=>{
          if(res.data.success){
            this.$message.success(res.data.message);
          }else{
            this.$message.error(res.data.message);
          }
          this.searchList();
        })
      },
      //分页
      handleSizeChange(val) {
        this.pageSize=val;
        this.searchList();
      },
      handleCurrentChange(val) {
        this.currentPage=val;
        this.searchList();
      },
      //上传

      handleAvatarSuccess(res, file) {//上传完成后回调
        //alert(res.result.path);
        this.posterForm.picPath=res.result.path;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'||file.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG/png 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      foundTs:function () {
        this.$http.get("/education-website/eduTemplate/getList").then((res)=>{
          this.TsList=res.data.result;
        })
      }

    },
    mounted () {
      this.searchList();
      this.foundTs();
    },
    filters:{
      adTypeFit:function (val) {
        if(val==0){
          return '轮播图';
        }
        if(val==1){
          return '楼层广告';
        }
      }
    }
  }
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
